<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\CollageOrder;
use app\api\model\Orders;
use app\api\model\SpecGoods;
use think\Request;

class Order
{
    /**
     * @param Request $request
     * @return array
     * 验证生成订单数据
     */
     public function order(Request $request)
     {

         $where['user_id']=$request->param('user_id');
         //$user_data=
         $where['goods_id']=$request->param('goods_id'); //商品id
         $where['goods_nums']=$request->param('goods_nums'); //购买数量
         //数据库查询 判断是否有商品
         $goods_data=SpecGoods::specGoodsDetails($where['goods_id']);
         if (!$goods_data) {
             return json(['code' => 1, 'msg' => '商品信息有误请重新购买', 'data' => null]);
         }
         if ($goods_data['store_count']<$where['goods_nums']){
             return json(['code' => 1, 'msg' => '商品数量不足', 'data' => null]);
         }
         $where['goods_price']=$goods_data['goods_price']; //商品单价
         $where['goods_total']= (int)$where['goods_nums'] * (int)$goods_data['goods_price']; //商品总价
         $where['delivery_id']=$request->param('delivery_id'); //配送方式id
         $where['address_id']=$request->param('address_id');  //地址id
         $where['pay_id']=$request->param('pay_id');   //支付方式id
         $where['order_type']=$request->param('order_type'); //订单种类
         $where['collage_id']=$request->param('collage_id'); //团长id
         $where['status']=0;
         $where['remarks']=$request->param('remarks');  //订单备注
         $where['order_no']=self::orderNo($where['order_type']);
          self::createOrder($where);
     }

    /**
     * @param $where
     * 生成订单
     */
     public static function createOrder($where)
     {
         switch ($where['order_type']) {
             case 3:
                 $where['status']=12;
                 //生成拼团总订单 订单状态为拼团中
                 $order= Orders::createOrder($where);
                 //团长拼团记录
                 $where['status']=0;
                 $where['order_id']=$order->id;
                 CollageOrder::collageOrder($where);

                 break;
             case 4:
                 $where['status']=10;
                 Orders::createOrder($where);
                 break;
             case 5:
                 //拼团成员拼团记录
                 $where['status']=0;
                 CollageOrder::collageOrder($where);
                 break;
             default:
                 Orders::createOrder($where);
         }
     }
    //获取毫秒 13位
    public static function microsecond()
    {
        $t = explode(" ", microtime());
        $microsecond = round(round($t[1].substr($t[0],2,3)));
        return $microsecond;
    }
    public static function orderNo($order_type)
    {
        $time=self::microsecond();
        switch ($order_type) {
            case 1:
                $order_no='FX'.$time.rand(100,999);
                break;
            case 2:
                $order_no='HX'.$time.rand(100,999);
                break;
            case 3:
                $order_no='KT'.$time.rand(100,999);
                break;
            case 4:
                $order_no='KJ'.$time.rand(100,999);
                break;
            case 5:
                $order_no='PT'.$time.rand(100,999);
                break;
            default:
                $order_no='SQ'.$time.rand(100,999);
        }
        return $order_no;
    }
    /**
     * 支付订单
     */
    public function  paymentCheck(Request $request)
    {
        $user_id=$request->param('user_id');
        $order_id=$request->param('order_id'); //订单表
        //查询订单数据
        $order_data=Orders::detailsOrder($order_id);
        //判断订单状态 判断订单是否过期
        if (!$order_data['status'==0]){
            return json(['code' => 1, 'msg' => '订单状态不对', 'data' => null]);
        }
        if(($order_data['create_time']+60*15)<time()){
            self::updateOrderStatus($order_id,$status=7);
            return json(['code' => 1, 'msg' => '订单已过期', 'data' => null]);
        }
        $where['delivery_id']=$request->param('delivery_id'); //配送方式id
        $where['address_id']=$request->param('address_id');  //地址id
        $where['pay_id']=$request->param('pay_id');   //支付方式id
        //调用方法修改订单数据 将配送方式id  地址id  支付方式id 添加订单表里
        $res=Orders::updateOrder($order_id,$where);
        if ($res){
            //调用支付接口 参数:（用户id 订单id）  根据返回码判断支付状态  返回支付二维码

            //返回支付二维码
            return json(['code' => 1, 'msg' => '订单已过期', 'data' =>'支付二维码地址']);
        }else{
            return json(['code' => 1, 'msg' => '订单已过期', 'data' => null]);
        }
    }

    /**
     * 修改订单状态
     * @param $order_id
     * @param $status
     *
     */
    public function updateOrderStatus($order_id ,$status)
    {
        $where['status']=$status;
        $res=Orders::updateOrderStatus($order_id,$where);
    }
   /**
    * 退款
    */


}
